package com.huobao.yunying.activitys;

import java.util.List;
import java.util.Map;

import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.Types;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.PopupStrings;
import com.huobao.yunying.utils.Utils;
import com.huobao.zhangying.R;

import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 咨询代理
 * 
 * @author Administrator
 *
 */
public class Act_Zixuan_Daili extends HuobaoBaseActivity implements OnClickListener {

	// 选择咨询主题
	@ViewInject(R.id.tv_theme)
	private TextView tv_theme;
	// 关闭按钮
	@ViewInject(R.id.tv_close)
	private TextView tv_close;
	// 姓名
	@ViewInject(R.id.edt_name)
	private EditText edt_name;
	// 手机号码
	@ViewInject(R.id.edt_shoujihao)
	private EditText edt_shoujihao;
	// QQ号
	@ViewInject(R.id.edt_qq)
	private EditText edt_qq;
	// 地址
	@ViewInject(R.id.edt_address)
	private EditText edt_address;
	// 留言
	@ViewInject(R.id.edt_content)
	private EditText edt_content;
	// 选择快捷留言
	@ViewInject(R.id.tv_kuaijie_liuyan)
	private TextView tv_kuaijie_liuyan;

	private List<Types> titles, contents;
	private String goodsId, storeid;

	@Override
	protected int getContentView() {
		return R.layout.layout_zixun_daili;
	}

	@Override
	protected String getTitleText() {
		return null;
	}

	@Override
	protected void init() {
		goodsId = getIntent().getStringExtra("id");
		storeid = getIntent().getStringExtra("sid");
		mAbTitleBar.setVisibility(View.GONE);
		tv_close.setOnClickListener(this);
		tv_theme.setOnClickListener(this);
		tv_kuaijie_liuyan.setOnClickListener(this);
		getTypes();
	}

	@Override
	public void onClick(final View v) {
		switch (v.getId()) {
		case R.id.tv_close:
			this.finish();
			break;
		case R.id.tv_theme:
			PopupStrings.showPop(context, titles, v, new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					String str = titles.get(position).stext;
					((TextView) v).setText(str);
					PopupStrings.dismiss();
				}
			});
			break;
		case R.id.tv_kuaijie_liuyan:
			PopupStrings.showPop(context, contents, v, new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					String str = contents.get(position).stext;
					edt_content.setText(str);
					PopupStrings.dismiss();
				}
			});
			break;
		}
	}

	private void getTypes() {
		Map<String, String> map = HttpUtil.getParams("app", "goods", "act", "agentcon");
		Http.getInstant().sendRequest(context, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				jsonObject = jsonObject.getJSONObject("retval");
				titles = JSON.parseArray(jsonObject.getString("title"), Types.class);
				contents = JSON.parseArray(jsonObject.getString("content"), Types.class);
			}

			@Override
			public void onHttpError(String error) {
			}
		});
	}

	public void onSubmitClick(View v) {
		String title = tv_theme.getText().toString().trim();
		String name = edt_name.getText().toString().trim();

		String mobile = edt_shoujihao.getText().toString().trim();
		String qqnum = edt_qq.getText().toString().trim();
		String address = edt_address.getText().toString().trim();
		String content = edt_content.getText().toString().trim();
		if (title.equals("请选择咨询主题")) {
			Toast.makeText(context, "请选择主题", Toast.LENGTH_SHORT).show();
			return;
		}
		if (TextUtils.isEmpty(name)) {
			Toast.makeText(context, "请填写姓名", Toast.LENGTH_SHORT).show();
			return;
		}
		if (TextUtils.isEmpty(mobile) || Utils.isMobileNO(mobile) == false) {
			Toast.makeText(context, "请填写正确的手机号码", Toast.LENGTH_SHORT).show();
			return;
		}
		if (TextUtils.isEmpty(content)) {
			Toast.makeText(context, "请填写留言内容", Toast.LENGTH_SHORT).show();
			return;
		}
		Map<String, String> map = HttpUtil.getParams("app", "goods", "act", "message", "id", goodsId, "sid", storeid,
				"title", title, "name", name, "mobile", mobile, "qqnum", qqnum, "address", address, "content", content);
		Http.getInstant().sendRequest(context, map, new HttpListener() {
			
			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				String msg = jsonObject.getString("retval");
				Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
				finish();
			}
			
			@Override
			public void onHttpError(String error) {

			}
		});
	}
}
